package jsp.servlet;

import java.io.IOException;
import java.util.Map.Entry;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jsp.classes.Session;

/**
 * @author Remco Haszing
 * @author Aymeric Blok
 */
public class AddUser extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            Session session = (Session) request.getSession(true).getAttribute("user");
            int id = -1;
            int pid = -1;
            for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
                String k = entry.getKey();
                String[] v = entry.getValue();
                if (k.equals("id")) {
                    id = Integer.parseInt(v[0]);
                } else if (k.equals("pid")) {
                    pid = Integer.parseInt(v[0]);
                }
            }
            session.findEmployeeById(id).addProject(session.findProjectById(pid));

            response.getWriter().print("true");
        } catch (Exception e) {
            response.getWriter().print("false");
        }
    }
}
